package com.source.nioatguigu;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @Author wengqy
 * @Date 2021/1/12
 */
public class MyChannel {
    public static void main(String[] args) throws Exception {
        // copy();
        copyyy();

    }


    public static  void copyyy() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("1.avi"), StandardOpenOption.READ );
        FileChannel outChannel = FileChannel.open(Paths.get("110.avi"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE );
        MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMap = inChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
        byte[] dst =  new byte[inMap.limit()];
        inMap.get(dst);
        outMap.put(dst);
        inChannel.close();
        outChannel.close();

    }



    public static  void copy(){
        FileInputStream fis= null;
        FileOutputStream fos= null;
        FileChannel inChannel = null ;
        FileChannel outChannel = null;

        try {
            fis=new FileInputStream(new File("1.avi"));
            fos = new FileOutputStream(new File("11.avi"));
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();

            ByteBuffer buf  =  ByteBuffer.allocate(1024);

            while((inChannel.read(buf))!=-1){
                buf.flip();
                outChannel.write(buf);
                buf.clear();
            }



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
           if( outChannel !=null) {
               try {
                   outChannel.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

            if( inChannel !=null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if( fos !=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            if( fis !=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }







    public  static void sop() throws Exception{
        RandomAccessFile  read= new RandomAccessFile("1.txt","rw");
        FileChannel readChannel = read.getChannel();
        RandomAccessFile  write= new RandomAccessFile("11.txt","rw");
        FileChannel writeChannel = write.getChannel();


        ByteBuffer buffer = ByteBuffer.allocate(1024);
        System.out.println(buffer.limit());
        readChannel.read(buffer);
        System.out.println(buffer.limit());
        buffer.flip();
        System.out.println(new String(buffer.array(),0,buffer.limit()));
        System.out.println(buffer.limit());
        writeChannel.write(buffer);
        // writeChannel.close();
        //  readChannel.close();
    }
}
